﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MapControl;
using MapWinGIS;

namespace MapLib
{
    public partial class ucMap : UserControl
    {
        //*****************************************************
        //Draw grid to create 9x9 Coordinate
        private Coordinate9x9 _coord9x9;
        private bool showCoordinate9x9;

        public ucMap()
        {
            InitializeComponent();
            _coord9x9 = new Coordinate9x9(this);
        }

        public AxMapWinGIS.AxMap AxMap1
        {
            get { return axMap1; }
        }

        /// <summary>
        /// Show 9x9 coordinate
        /// </summary>
        public bool ShowCoordinate9x9
        {
            get
            {
                return showCoordinate9x9;
            }
            set
            {
                showCoordinate9x9 = value;
            }
        }

        /// <summary>
        /// Show 9x9 coordinate
        /// </summary>
        public Coordinate9x9 Coord9x9
        {
            get
            {
                return _coord9x9;
            }
            set
            {
                _coord9x9 = value;
            }
        }

        /// <summary>
        /// Khoảng cách zoom hiển thị bản đồ (đơn vị là km)
        /// </summary>
        public double Altitude
        {
            get
            {
                return AxMap1.CurrentScale / 1000;
            }
        }

        

        private void ucMap_Load(object sender, EventArgs e)
        {
            InitMap();
        }

        public event AxMapWinGIS._DMapEvents_OnDrawBackBufferEventHandler AxMap_DrawBackBuffer;
        private void axMap1_OnDrawBackBuffer(object sender, AxMapWinGIS._DMapEvents_OnDrawBackBufferEvent e)
        {
            if (AxMap_DrawBackBuffer != null)
                AxMap_DrawBackBuffer(sender, e);

            Graphics myGraphics = Graphics.FromHdc(new IntPtr(e.backBuffer));

            //9x9 coordinate
            _coord9x9.Init();
            if (showCoordinate9x9)
                _coord9x9.Draw(myGraphics);

            //myGraphics.ReleaseHdc(hdc);
            myGraphics.Dispose();

        }

        public void InitMap()
        {
            //Cho phép dùng scroll để thu phóng bản đồ
            //AxMap1.MousewheelSupport = MapXLib.MousewheelSupportConstants.miMousewheelNoAutoScroll;
            //AxMap1.MousewheelSupport = MapXLib.MousewheelSupportConstants.miFullMousewheelSupport;//.miNoMousewheelSupport;//.miMousewheelNoAutoScroll;
            //AxMap1.GeoSet = string.Format(@"{0}\Maps\BanDo.gst", Application.StartupPath);
            //AxMap1.Title.Visible = false;


            //AxMap1.CurrentTool = MapXLib.ToolConstants.miArrowTool;
            //AxMap1.NumericCoordSys = AxMap1.DisplayCoordSys;
            //AxMap1.PaperUnit = MapXLib.PaperUnitConstants.miPaperUnitPoint;
            //AxMap1.MapUnit = MapXLib.MapUnitConstants.miUnitMeter;//.miUnitKilometer;
            //AxMap1.Layers.AddUserDrawLayer("Symbols", 1);

            //ViewDefaultMap();

            //double x = AxMap1.Layers["Symbols"].CoordSys.Bounds.XMin;
            //double y = AxMap1.Layers["Symbols"].CoordSys.Bounds.YMin;

            //double zoom = AxMap1.Zoom;
            //double cX = AxMap1.CenterX;
            //double cY = AxMap1.CenterY;
            //AxMap1.CreateCustomTool(3, MapXLib.ToolTypeConstants.miToolTypePoint,
            //                            MapXLib.CursorConstants.miArrowQuestionCursor);

            axMap1.MapUnits = tkUnitsOfMeasure.umMeters;
            axMap1.CurrentScale = 500000.0;
        }

        public override void Refresh()
        {
            //AxMap1.Refresh();
            AxMap1.Redraw();

        }

        public event AxMapWinGIS._DMapEvents_MouseDownEventHandler AxMap_MouseDownEvent;
        private void axMap1_MouseDownEvent(object sender, AxMapWinGIS._DMapEvents_MouseDownEvent e)
        {
            if (AxMap_MouseDownEvent != null)
                AxMap_MouseDownEvent(sender, e);

            //if left mouse click
            if (e.button == 1)
            {

            }
            else if (e.button == 2)
            {
                //Nếu đang ở kéo bản đồ thì chuyển sang mũi tên
                if (AxMap1.CursorMode == tkCursorMode.cmPan)
                {
                    AxMap1.CursorMode = tkCursorMode.cmSelection;
                }
            }
        }

        public event AxMapWinGIS._DMapEvents_MouseMoveEventHandler AxMap_MouseMoveEvent;
        private void axMap1_MouseMoveEvent(object sender, AxMapWinGIS._DMapEvents_MouseMoveEvent e)
        {
            //Nếu đang nhấn chuột trái mà move
            if (e.button == 1)
            {
                //Nếu đang ở tool mũi tên thì chuyển sang pan
                if (AxMap1.CursorMode == tkCursorMode.cmSelection)
                {
                    AxMap1.CursorMode = tkCursorMode.cmPan;
                }
            }
            if (AxMap_MouseMoveEvent != null)
                AxMap_MouseMoveEvent(sender, e);

        }

        public event AxMapWinGIS._DMapEvents_MouseUpEventHandler AxMap_MouseUpEvent;
        private void axMap1_MouseUpEvent(object sender, AxMapWinGIS._DMapEvents_MouseUpEvent e)
        {

        }

    }
}
